'''
Created on 2019年1月3日

@author: allan
'''
import requests

from utils.config.readConfig import ReadConfig
import json
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


localReadConfig= ReadConfig()
class HttpAction(object):
    '''
    classdocs
    '''


    def __init__(self, httpname = "HTTPluxury"):
        '''
        Constructor
        '''
        self.httpname=httpname
        self.scheme = localReadConfig.get_http(self.httpname,"scheme")
        self.host = localReadConfig.get_http(self.httpname,"baseurl")
        self.port = localReadConfig.get_http(self.httpname,"port")
        self.timeout = localReadConfig.get_http(self.httpname,"timeout")

        self.headers = {}
        self.params = {}
        self.data = {}
        self.url = None
        self.files = {}
        self.state = 0

    #post方法 返回 json字符串
    def callApi_get(self,url="",header={},data={},desc =''):

        try:
            
            getdata=self.commonget(url,header,data)
            data = getdata.json()

            print("*** " +desc + json.dumps(data,ensure_ascii=False) + " success")
            
            return data
        except AssertionError:
            print("!!! get failed ")


    #post方法 返回 json字符串
    def callApi_post(self,url="",header={},data={},desc =''):

        try:
            
            getdata=self.commonpost(url,header,data)
            data = getdata.json()
            print("*** "+desc  + json.dumps(data,ensure_ascii=False) + " success")
            
            return data
        except AssertionError:
            print("!!! failed " + " "+self.mobile + self.message)

    #post方法 返回 json字符串
    def callApi_postjson(self,url="",header={"Content-Type":"application/json","charset":"utf-8"},data={},desc =''):

        try:
            
            self.set_url(url)
            
            self.set_data(data)
            self.set_headers(header)
            #getdata=confighttp.commonpost(url,header,data)
            getdata=self.postWithJson()
            data = getdata.json()
            print("**"+desc + json.dumps(data,ensure_ascii=False) + " success")
            
            return data
        except AssertionError:
            print("!!! failed "+desc  + " "+self.mobile + self.message)



    def set_url(self, url):
        """
        set url
        :param: interface url
        :return:
        """
        self.url = self.scheme+'://'+self.host+":"+self.port+url

    def set_headers(self, header):
        """
        set headers
        :param header:
        :return:
        """
        self.headers = header

    def set_params(self, param):
        """
        set params
        :param param:
        :return:
        """
        self.params = param

    def set_data(self, data):
        """
        set data
        :param data:
        :return:
        """
        self.data = data

    # defined http get method
    def get(self):
        """
        defined get method
        :return:
        """
        try:
            response = requests.get(self.url, headers=self.headers, params=self.params, timeout=float(self.timeout))
            return response
        except requests.ConnectTimeout:
            print("get Time out!")
            return None

    # defined http post method
    # include get params and post data
    # uninclude upload file
    def post(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, params=self.params, data=self.data, timeout=float(self.timeout),verify=False)
            return response

        except requests.ConnectTimeout:
            print("post Time out!")
            return None

    # defined http post method
    # include upload file
    def postWithFile(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, data=self.data, files=self.files, timeout=float(self.timeout))
            return response
        except requests.ConnectTimeout:
            print("postWithFile Time out!")
            return None

    # defined http post method
    # for json
    def postWithJson(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, json=self.data, timeout=float(self.timeout))
            return response
        except requests.ConnectTimeout:
            print("postWithJson Time out!")
            return None

    def commonget(self, url, header={},params={}):
        """
        :param url:
        :param header:
        :param data:
        :return: status, message, result
        """
        self.set_url(url)
        self.set_headers(header)
        self.set_params(params)
        re=self.get()
        return re

    def commonpost(self, url, header={}, data={}):
        """
        :param url:
        :param header:
        :param data:
        :return: status, message, result
        """
        self.set_url(url)
        self.set_headers(header)
        self.set_data(data)
        re=self.post()
        return re